import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import { viteMockServe } from 'vite-plugin-mock'

// https://vitejs.dev/config/


export default defineConfig((commandJson) => {
  //
  let { command } = commandJson
  let isEnable: boolean = (command=='serve')
  const _Version = new Date().getTime();
  return {
    plugins: [
      vue(),
      vueJsx(),
      viteMockServe({
        mockPath:'./mock',
        localEnabled:isEnable,
      }),
    ],
    css: {
      preprocessorOptions: {
        //define global scss variable
        scss: {
          javascriptEnabled: true,
          additionalData: `
            @import "@/assets/common.scss";
          ` // 全局变量导入
        }
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      port: 3000,
      proxy: {
        '/api': {
          target: 'http://172.22.11.51:8000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    build: {
      outDir: 'dist',
      // assetsDir: 'yt',
      rollupOptions: {
        output: {
          chunkFileNames: 'yt/js/[name]-[hash]'+_Version+'.js',
          entryFileNames: 'yt/js/[name]-[hash]'+_Version+'.js',
          assetFileNames: 'yt/[ext]/[name]-[hash]'+_Version+'.[ext]',
        },
      },
    },
  }
})
